using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Windows.Forms;

namespace IPAMS.Views
{
    /// <summary>
    /// This is an editor for the Taxes table. It enables loading from a file,
    ///saving to a file and more
    /// </summary>
    public partial class TaxesEditor : UserControl
    {
        public DSIPAMS.DTTaxesDataTable taxes;
        string[] taxesHeaders;

        /// <summary>
        /// Initializes a new instance of the <see cref="TaxesEditor"/> class.
        /// </summary>
        public TaxesEditor()
        {
            InitializeComponent();
            InitializeComponentExtneded();
        }

        /// <summary>
        /// Initializes the component extneded.
        /// </summary>
        private void InitializeComponentExtneded()
        {
            taxes = new DSIPAMS.DTTaxesDataTable();
            dgvTaxes.DataSource = taxes;
        }

        /// <summary>
        /// Handles the DataSourceChanged event of the dgvCash control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void dgvCash_DataSourceChanged(object sender, EventArgs e)
        {
            dgvTaxes.Refresh();
        }

        /// <summary>
        /// Loads the data from a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        /// <returns>a DataTable with the file data</returns>
        public DataTable LoadData(string dataFilePath)
        {

            if (File.Exists(dataFilePath))
            {
                StreamReader sr = new StreamReader(dataFilePath);
                taxesHeaders = sr.ReadLine().Split(',');
                ;
                while (true)
                {
                    string tax = sr.ReadLine();
                    if (tax == string.Empty || tax == null) break;
                    string[] taxInfo = tax.Split(',');
                    if (taxInfo.Length != 5) continue;
                   CultureInfo ci = new CultureInfo("he-IL");
                   
                  taxes.AddDTTaxesRow(taxInfo[0], taxInfo[1], Convert.ToDouble(taxInfo[2]),
                                        DateTime.Parse(taxInfo[3],ci.DateTimeFormat), Convert.ToDateTime(taxInfo[4]));
                }
                sr.Close();
                Text = dataFilePath;

                taxes.AcceptChanges();
                dgvTaxes.DataSource = taxes;

                for (int index = 0; index < taxesHeaders.Length; index++)
                {
                    dgvTaxes.Columns[index].HeaderText = taxesHeaders[index];
                }
                foreach (DataGridViewColumn column in dgvTaxes.Columns)
                {
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                }
            }

            return taxes;
        }

        /// <summary>
        /// Saves the data to a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        public void SaveData(string dataFilePath)
        {
            StreamWriter sw = new StreamWriter(dataFilePath);
            string fileContent = taxesHeaders + Environment.NewLine;
            foreach (DSIPAMS.DTTaxesRow tax in taxes)
            {
                fileContent += string.Format("{0},{1},{2},{3},{4}" + Environment.NewLine, tax[0], tax[1], tax[2], tax[3],tax[4]);
            }
            sw.Write(fileContent);
            sw.Close();

        }

    }
    
}
